package com.halko.PR.pr.detect;
import static org.bytedeco.javacpp.opencv_highgui.imwrite;

import java.util.Vector;

import org.bytedeco.javacpp.opencv_core.Mat;
import com.halko.PR.pr.core.CoreFunc;

/**
 * 车牌检测
 * @author halko
 * @create 2021-03-26 10:21
 */
public class PlateDetection {
    private PlateLocation plateLocation = new PlateLocation();

    private PlateJudgement plateJudgement = new PlateJudgement();

    private CoreFunc coreFunc = new CoreFunc();

    /**
     *
     * @param src
     * @param resultVec 候选车牌的图块集合
     * @return 错误数字
     *       0：车牌检测成功
     *      -1：Mat里面没有候选车牌
     *      -2：没有检测到车牌
     */
    public int plateDetect(Mat src, Vector<Mat> resultVec) {
        Vector<Mat> matVec = plateLocation.plateLocate(src);

        if (0 == matVec.size()) {
            return -1;
        }

        if (0 != plateJudgement.plateJudge(matVec, resultVec)) {
            return -2;
        }

        if (getPDDebug()) {
            for (int i = 0; i < resultVec.size(); i++) {
                Mat img = resultVec.get(i);
                String str = "C:\\Users\\halko\\Desktop\\VuePR\\src\\assets\\tmp\\judge\\plate_judge_result_" + Integer.valueOf(i).toString() + ".jpg";
                imwrite(str, img);
                // 判断车牌
                CoreFunc.Color color = coreFunc.getPlateType(img, true);
                System.out.println(color);
            }
        }

        return 0;
    }

    /**
     * 获取调试模式
     *
     * @return
     */
    private boolean getPDDebug() {
        return plateLocation.getDebug();
    }

    /**
     * 是否开启调试模式
     *
     * @param pdDebug
     */
    public void setPDDebug(boolean pdDebug) {
        plateLocation.setDebug(pdDebug);
    }
}
